<template>
    <div class="p-2">
      <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
        <div class="search" v-show="showSearch">
          <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
            <el-form-item label="所属档案" prop="recordId">
              <el-select v-model="form.recordId" placeholder="请选择档案" clearable style="width: 240px" @keyup.enter="handleQuery">
                <el-option v-for="(value,index) in recordList" :key="index" :label="value.title" :value="value.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="所属班级" prop="lightenId">
              <el-select v-model="queryParams.lightenId" placeholder="请选择班级" clearable style="width: 240px" @keyup.enter="handleQuery">
                <el-option v-for="(value,index) in lightenList" :key="index" :label="value.name" :value="value.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </transition>
  
      <el-card shadow="never">
        <template #header>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['school:score:export']">导出</el-button>
            </el-col>
              <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
        </template>
  
        <el-table v-loading="loading" :data="scoreList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="" align="center" prop="id" v-if="false" />
          <el-table-column label="学生名称" align="center" prop="name" />
          <el-table-column label="学号" align="center" prop="studentNumber" />
          <el-table-column label="科目" align="center" prop="subject" />
          <el-table-column label="成绩" align="center" prop="score" />
          <el-table-column label="科目排名" align="center" prop="subjectRanking" />
          <el-table-column label="班级排名" align="center" prop="classRanking" />
          <el-table-column label="学校排名" align="center" prop="schoolRanking" />
          <el-table-column label="联考排名" align="center" prop="jointRanking" />
          <el-table-column label="所属档案" align="center" prop="recordId" />
          <el-table-column label="所属班级" align="center" prop="lightenId" />
          <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-tooltip content="修改" placement="top">
                <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['school:score:edit']"></el-button>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['school:score:remove']"></el-button>
              </el-tooltip>
            </template>
          </el-table-column> -->
        </el-table>
  
        <pagination
            v-show="total>0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
        />
      </el-card>
      <!-- 添加或修改成绩对话框 -->
      
      <!-- 用户导入对话框 -->
    
    </div>
  </template>
  
  <script setup name="Score" lang="ts">
  import { listScore } from '@/api/school/statistics';
  import { ScoreVO, ScoreQuery, } from '@/api/school/statistics/types';
  import { ScoreForm } from '@/api/school/score/types';
  import { globalHeaders } from "@/utils/request";
  import { findLightenByUserId } from "@/api/school/lighten";
  import { LightenVO } from '@/api/school/lighten/types';
  import { listAllRecord } from '@/api/school/record';
  import { RecordVO } from '@/api/school/record/types';

  const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  
  const scoreList = ref<ScoreVO[]>([]);
  const lightenList = ref<LightenVO[]>([]);
  const recordList = ref<RecordVO[]>([]);
  const buttonLoading = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref<Array<string | number>>([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  /*** 用户导入参数 */
  const upload = reactive<ImportOption>({
    // 是否显示弹出层（用户导入）
    open: false,
    // 弹出层标题（用户导入）
    title: "",
    // 是否禁用上传
    isUploading: false,
    // 是否更新已经存在的用户数据
    updateSupport: 0,
    // 设置上传的请求头部
    headers: globalHeaders(),
    // 上传的地址
    url: import.meta.env.VITE_APP_BASE_API + "/school/score/importData",
    //班级
    lightenId: null,
    //档案
    recordId: null
  })
  const queryFormRef = ref<ElFormInstance>();
  const scoreFormRef = ref<ElFormInstance>();
  const scoreImportRef = ref<ElFormInstance>();
  const uploadRef = ref<ElUploadInstance>();
  const dialog = reactive<DialogOption>({
    visible: false,
    title: ''
  });

  const initFormData: ScoreForm = {
    id: undefined,
    name: undefined,
    studentNumber: undefined,
    subject: undefined,
    score: undefined,
    subjectRanking: undefined,
    classRanking: undefined,
    schoolRanking: undefined,
    jointRanking: undefined,
    recordId: undefined,
    lightenId: undefined,
  }
  const data = reactive<PageData<ScoreForm, ScoreQuery>>({
    form: {...initFormData},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      name: undefined,
      studentNumber: undefined,
      subject: undefined,
      score: undefined,
      subjectRanking: undefined,
      classRanking: undefined,
      schoolRanking: undefined,
      jointRanking: undefined,
      recordId: undefined,
      lightenId: undefined,
      params: {
      }
    },
    rules: {
      id: [
        { required: true, message: "不能为空", trigger: "blur" }
      ],
      name: [
        { required: true, message: "学生名称不能为空", trigger: "blur" }
      ],
      studentNumber: [
        { required: true, message: "学号不能为空", trigger: "blur" }
      ],
      subject: [
        { required: true, message: "科目不能为空", trigger: "blur" }
      ],
      score: [
        { required: true, message: "成绩不能为空", trigger: "blur" }
      ],
      subjectRanking: [
        { required: true, message: "科目排名不能为空", trigger: "blur" }
      ],
      classRanking: [
        { required: true, message: "班级排名不能为空", trigger: "blur" }
      ],
      schoolRanking: [
        { required: true, message: "学校排名不能为空", trigger: "blur" }
      ],
      jointRanking: [
        { required: true, message: "联考排名不能为空", trigger: "blur" }
      ],
      recordId: [
        { required: true, message: "所属档案不能为空", trigger: "blur" }
      ],
    }
  });
  const ruless = {
    recordId: [
      { required: true, message: "所属档案不能为空", trigger: "blur" }
    ],
    lightenId: [
      { required: true, message: "所属班级不能为空", trigger: "blur" }
    ]
  }
  const { queryParams, form} = toRefs(data);
  
  /** 查询成绩列表 */
  const getList = async () => {
    loading.value = true;
    const res = await listScore(queryParams.value);
    scoreList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  }

  /** 查询班级 */
  const getLighten = async () => {
    const res = await findLightenByUserId();
    lightenList.value = res.data;
  }

  /** 查询档案 */
  const getRecord = async () => {
    const res = await listAllRecord();
    recordList.value = res.data;
  }
  
  /** 取消按钮 */
  const cancel = () => {
    reset();
    dialog.visible = false;
  }
  
  /** 表单重置 */
  const reset = () => {
    form.value = {...initFormData};
    scoreFormRef.value?.resetFields();
  }
  
  /** 搜索按钮操作 */
  const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
  }
  
  /** 重置按钮操作 */
  const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
  }
  
  /** 多选框选中数据 */
  const handleSelectionChange = (selection: ScoreVO[]) => {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }
  
  
  /** 导出按钮操作 */
  const handleExport = () => {
    proxy?.download('school/score/export', {
      ...queryParams.value
    }, `score_${new Date().getTime()}.xlsx`)
  }

  /** 提交上传文件 */
  function submitFileForm() {
    scoreImportRef.value?.validate(async (valid: boolean) => {
      if (valid) {
        uploadRef.value?.submit();
      }
    });
    
  }
  onMounted(() => {
    getList();
    getLighten();
    getRecord();
  });
  </script>
  